﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using System.Web.Security;

namespace Common
{
    public class SendEmailHelper
    {
        #region 发送邮件的方法(Web)
        /// <summary>
        /// 发送邮件的方法
        /// </summary>
        /// <param name="toMail">收件人(收件人不能为空,多个人用分号隔开，注意是英文的分号)</param>
        /// <param name="caoSong">抄送人(没有传空，多个人用分号隔开，注意是英文的分号)</param>
        /// <param name="Subject">主题(不能为空且不超过100字，超过只取前100个字)</param>
        /// <param name="body">内容(内容不能为空，为空直接过滤)</param>
        /// <param name="keyCode">参数</param>
        /// <param name="isHtml">邮件格式是否以Html显示</param>
        public static void SendEMail(string toMail, string caoSong, string subject, string body, string keyCode, bool isHtml)
        {
            #region 参数验证
            string key = "_tcEmail_haha";
            string mykeyCode = FormsAuthentication.HashPasswordForStoringInConfigFile(key + toMail, "MD5");
            if (mykeyCode != keyCode)
            {
                Logger.WriteLog("keyCode不匹配");
                return;
            }
            if (string.IsNullOrEmpty(toMail))
            {
                return;
            }
            if (string.IsNullOrEmpty(subject))
            {
                return;
            }
            else
            {
                if (subject.Length > 100)
                {
                    subject = subject.Substring(0, 100);
                }
            }
            if (string.IsNullOrEmpty(body))
            {
                return;
            }
            #endregion
            // 创建邮件消息 
            System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
            mailMsg.From = "automail@17u.com";//源邮件地址 
            mailMsg.To = toMail;//目的邮件地址，也就是发给我哈 
            mailMsg.Cc = caoSong; //抄送
            mailMsg.Subject = subject;//发送邮件的标题 
            mailMsg.Body = body;//发送邮件的内容 
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.Priority = System.Web.Mail.MailPriority.High;
            if (isHtml)
            {
                mailMsg.BodyFormat = System.Web.Mail.MailFormat.Html;
            }
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailMsg.From);
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "AUTOmail");

            //SMTP地址 
            System.Web.Mail.SmtpMail.SmtpServer = "mail.17u.com";
            //开始发送邮件 
            System.Web.Mail.SmtpMail.Send(mailMsg);
        }
        #endregion

        #region 发送邮件的方法(Web)
        /// <summary>
        /// 发送邮件的方法
        /// </summary>
        /// <param name="toMail">收件人(收件人不能为空,多个人用分号隔开，注意是英文的分号)</param>
        /// <param name="caoSong">抄送人(没有传空，多个人用分号隔开，注意是英文的分号)</param>
        /// <param name="Subject">主题(不能为空且不超过100字，超过只取前100个字)</param>
        /// <param name="body">内容(内容不能为空，为空直接过滤)</param>
        /// <param name="keyCode">参数</param>
        /// <param name="isHtml">邮件格式是否以Html显示</param>
        public static void SendEMail(string toMail, string caoSong, string subject, string body,string file, string keyCode, bool isHtml)
        {
            #region 参数验证
            string key = "_tcEmail_haha";
            string mykeyCode = FormsAuthentication.HashPasswordForStoringInConfigFile(key + toMail, "MD5");
            if (mykeyCode != keyCode)
            {
                Logger.WriteLog("keyCode不匹配");
                return;
            }
            if (string.IsNullOrEmpty(toMail))
            {
                return;
            }
            if (string.IsNullOrEmpty(subject))
            {
                return;
            }
            else
            {
                if (subject.Length > 100)
                {
                    subject = subject.Substring(0, 100);
                }
            }
            if (string.IsNullOrEmpty(body))
            {
                return;
            }
            #endregion
            // 创建邮件消息 
            System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
            mailMsg.From = "automail@17u.com";//源邮件地址 
            mailMsg.To = toMail;//目的邮件地址，也就是发给我哈 
            mailMsg.Cc = caoSong; //抄送
            mailMsg.Subject = subject;//发送邮件的标题 
            mailMsg.Body = body;//发送邮件的内容 
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.Priority = System.Web.Mail.MailPriority.High;
            if (isHtml)
            {
                mailMsg.BodyFormat = System.Web.Mail.MailFormat.Html;
            }
            if (!string.IsNullOrEmpty(file))
            {
                mailMsg.Attachments.Add(new Attachment(file));
            }
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailMsg.From);
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "AUTOmail");

            //SMTP地址 
            System.Web.Mail.SmtpMail.SmtpServer = "mail.17u.com";
            //开始发送邮件 
            System.Web.Mail.SmtpMail.Send(mailMsg);
        }
        #endregion

        #region 发送电子邮件(使用.net自带组件发送)
        /// <summary>
        /// 发送电子邮件(使用.net自带组件发送)
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">邮件内容</param>
        public static void SendEMail_Net(string toMail, string caoSong, string subject, string body, string keyCode, bool isHtml)
        {
            #region 参数验证
            string key = "_tcEmail_haha";
            string mykeyCode = FormsAuthentication.HashPasswordForStoringInConfigFile(key + toMail, "MD5");
            if (mykeyCode != keyCode)
            {
                Logger.WriteLog("keyCode不匹配");
                return;
            }
            if (string.IsNullOrEmpty(toMail))
            {
                return;
            }
            if (string.IsNullOrEmpty(subject))
            {
                return;
            }
            else
            {
                if (subject.Length > 100)
                {
                    subject = subject.Substring(0, 100);
                }
            }
            if (string.IsNullOrEmpty(body))
            {
                return;
            }
            #endregion
            try
            {
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                smtpClient.Host = "mail.17u.com";//指定SMTP服务器
                smtpClient.Credentials = new System.Net.NetworkCredential("automail@17u.com", "AUTOmail");//用户名和密码

                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage("automail@17u.com", toMail);
                mailMessage.Subject = subject;//主题
                mailMessage.Body = body;//内容
                mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码
                if (isHtml)
                {
                    mailMessage.IsBodyHtml = true;//设置为HTML格式
                }
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                Logger.WriteLog("发送邮件异常",ex);
            }
        }
        #endregion

        #region 发送电子邮件(使用.net自带组件发送)
        /// <summary>
        /// 发送电子邮件(使用.net自带组件发送)
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">邮件内容</param>
        public static void SendEMail_Net(string toMail, string caoSong, string subject, string body,List<Attachment> listAtt , string keyCode, bool isHtml)
        {
            #region 参数验证
            string key = "_tcEmail_haha";
            string mykeyCode = FormsAuthentication.HashPasswordForStoringInConfigFile(key + toMail, "MD5");
            if (mykeyCode != keyCode)
            {
                Logger.WriteLog("keyCode不匹配");
                return;
            }
            if (string.IsNullOrEmpty(toMail))
            {
                return;
            }
            if (string.IsNullOrEmpty(subject))
            {
                return;
            }
            else
            {
                if (subject.Length > 100)
                {
                    subject = subject.Substring(0, 100);
                }
            }
            if (string.IsNullOrEmpty(body))
            {
                return;
            }
            #endregion
            try
            {
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                smtpClient.Host = "mail.17u.com";//指定SMTP服务器
                smtpClient.Credentials = new System.Net.NetworkCredential("automail@17u.com", "AUTOmail");//用户名和密码

                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage("automail@17u.com", toMail);
                mailMessage.Subject = subject;//主题
                mailMessage.Body = body;//内容
                if (listAtt.Count!=0)
                {
                    foreach (Attachment list in listAtt)
                    {
                        mailMessage.Attachments.Add(list);
                    }
                }
                mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码
                if (isHtml)
                {
                    mailMessage.IsBodyHtml = true;//设置为HTML格式
                }
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                Logger.WriteLog("发送邮件异常", ex);
            }
        }
        #endregion
    }
}
